package com.bkbw.live.battle.net.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LogLevel;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.bkbw.live.battle.actor.ConstActors;
import com.bkbw.live.battle.net.codec.NetCodecFactory;
import com.bkbw.live.battle.net.message.MessageCenter;

public class FBServer {

	public static void main(String[] args) throws IOException {
		//化消息管理器
		MessageCenter.getInstance().init();
		//初始化Actors
		ConstActors.init();
		
		NioSocketAcceptor acceptor = new NioSocketAcceptor();
        acceptor.setBacklog(511);
        acceptor.setReuseAddress(true);
        //filter
        LoggingFilter loggingFilter = new LoggingFilter();  
        loggingFilter.setMessageSentLogLevel(LogLevel.NONE);
        loggingFilter.setMessageReceivedLogLevel(LogLevel.NONE);
        loggingFilter.setSessionClosedLogLevel(LogLevel.NONE);  
        loggingFilter.setSessionCreatedLogLevel(LogLevel.NONE);  
        loggingFilter.setSessionOpenedLogLevel(LogLevel.NONE);   
        loggingFilter.setMessageReceivedLogLevel(LogLevel.NONE);
        acceptor.getFilterChain().addLast( "logger", loggingFilter);//日志过滤器
        //ProtocalCodecFilter过滤器非常重要，因为TCP/IP协议只能保证字节都能够按顺序全部传输，但是可能一条消息被分成多次事件，或者多条消息被合并成一次事件触发,
        acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter(new NetCodecFactory()));  
        acceptor.setHandler(new FBServerHandler());  
        //设置一些配置属性
        acceptor.getSessionConfig().setReadBufferSize( 2048 );  
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );  
        
        int port = Integer.parseInt(args[0]);
        acceptor.bind(new InetSocketAddress(port)); 
	}
}
